package com.example.educate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.educate.mapper.CourseMapper;
import com.example.educate.model.vod.Course;
import com.example.educate.model.vod.CourseDescription;
import com.example.educate.model.vod.Subject;
import com.example.educate.model.vod.Teacher;
import com.example.educate.service.CourseDescriptionService;
import com.example.educate.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educate.service.SubjectService;
import com.example.educate.service.TeacherService;
import com.example.educate.vo.vod.CourseFormVo;
import com.example.educate.vo.vod.CoursePublishVo;
import com.example.educate.vo.vod.CourseQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author AT
 * @since 2022-09-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private CourseMapper courseMapper;
//    private CourseMapper courseMapper;

    @Override
    public Map<String, Object> findPageCourse(Page<Course> page1, CourseQueryVo courseQueryVo) {

        String title = courseQueryVo.getTitle();
        Long teacherId = courseQueryVo.getTeacherId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        Long subjectId = courseQueryVo.getSubjectId();
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        Page<Course> coursePage = baseMapper.selectPage(page1, wrapper);
        long totalPage = coursePage.getPages();
        long totalCount = coursePage.getTotal();
        List<Course> list = coursePage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("page",totalPage);
        map.put("total",totalCount);
        map.put("list",list);
        list.stream().forEach(item->{
            this.getNameId(item);
        });
        return map;
    }
    //添加课程返回课程id
    @Override
    public Long saveCourseInFo(CourseFormVo courseFormVo) {
        //添加课程的基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.insert(course);
        //添加课程的描述信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(course.getId());
        return course.getId();
    }
    //根据课程id查询基本信息
    @Override
    public CourseFormVo getCourseInfoById(Long id) {
        Course course = baseMapper.selectById(id);
        if(course==null){
            return null;
        }
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        //描述信息
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        if(courseDescription!=null){
            courseFormVo.setDescription(courseDescription.getDescription());
        }
        return courseFormVo;
    }
    //修改课程信息
    @Override
    public void updateCourseId(CourseFormVo courseFormVo) {
        //修改课程信息
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);

        CourseDescription description = new CourseDescription();
        description.setDescription(courseFormVo.getDescription());
        description.setId(course.getId());
//        courseDescriptionService.updateById(description);
    }
    //课程发布
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return courseMapper.selectCoursePublishVoById(id);
    }
    //课程最终发布
    @Override
    public void publishCourse(Long id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1);
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
    }

    private Course getNameId(Course course) {
        //根据讲师id获取讲师名称
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher!=null){
            course.getParam().put("teacherName",teacher.getName());
        }
        //根据课程分类id获取课程分类名称
        Subject courseSubject = subjectService.getById(course.getSubjectId());
        if(courseSubject!=null){
            course.getParam().put("subjectParentTitle",courseSubject.getTitle());
        }
        //根据课程分类id获取课程分类名称
        Subject SubjectTwo = subjectService.getById(course.getSubjectId());
        if(courseSubject!=null){
            course.getParam().put("subjectTitle",SubjectTwo.getTitle());
        }
        return course;
    }
}
